<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>title</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css"/>
    <style>
    	body{
    		
    	}
    	.none_data{
    		width:100%;
    		text-align: center;
    	}
    </style>
</head>
<body>
</body>
<script data-main="../../script/main" src="../../script/require.js"></script>
<script type="text/javascript">
	apiready = function(){
		require(["zepto","common"],function(){
			openDB();		
		})
	};
	
	
	function openDB() {
		db = api.require('db');
		dbName = "zhibo_db";
		db.openDatabase({
			name : dbName,
			path : 'fs://zhibo_db.db'
		}, function(ret, err) {
			if (!ret.status) {
				return;
			}
			/* 创建数据表 */
			var sql = "CREATE TABLE  IF NOT EXISTS `zhibo_messages`(`id` varchar(32), `content` varchar(255),`fromuser` varchar(100),  `datetime` timestamp  NOT NULL DEFAULT (datetime('now','localtime')))";
			db.executeSql({
				name : dbName,
				sql : sql
			}, function(ret, err) {
				if (!ret.status) {
					return;
				}
				/* 查询数据 */
				var sql = "SELECT * FROM zhibo_messages order by datetime desc";
				db.selectSql({
					name : dbName,
					sql : sql
				}, function(ret, err) {
					if (!ret.status) {
						api.toast({
							msg : err.msg
						})
						return;
					}
					var json = JSON.stringify(ret.data);
					var jsonRes = JSON.parse(json);
					var listviewObject = [];
					for (var one in jsonRes) {
						var str = {};
						for (var key in jsonRes[one]) {
							str.title = jsonRes[one]["content"];
							str.subTitle = jsonRes[one]["fromuser"]+"  "+jsonRes[one]["datetime"];
							str.uid = jsonRes[one]["id"];
						}
						listviewObject.push(str);
					}
					obj = api.require('UIListView');
					if (listviewObject.length > 0) {
						obj.open({
							rect : {
								x : 0,
								y : 0,
								w : api.winWidth
							},
							rightBtns : [{
								bgColor : '#36b05e',
								activeBgColor : '#388e8e',
								width : 70,
								title : '删除',
								titleSize : 12,
								titleColor : '#fff',
								icon : '',
								iconWidth : 20
							}],
							styles : {
								borderColor : '#EDEDED',
								item : {
									bgColor : 'rgba(255,0,0,0)',
									activeBgColor : '#F5F5F5',
									height : 67
								}
							},
							data : listviewObject,
							fixedOn : api.frameName
						}, function(ret, err) {
							if (ret.eventType == 'clickRightBtn') {
								var id = listviewObject[ret.index].uid;
								deleteItem(id);
							}
						});
					} else {
						$("body").append(buildNothingDiv());
					}
				});
			});
		});
	}

	var deleteItem = function(id) {
		var db = api.require('db');
		dbName = "zhibo_db";
		db.openDatabase({
			name : dbName,
			path : 'fs://zhibo_db.db'
		}, function(ret, err) {
			if (!ret.status) {
				return;
			}
			var sql = "delete from zhibo_messages where id ='" + id + "'";
			db.executeSql({
				name : dbName,
				sql : sql
			}, function(ret, err) {
				obj.close();
				window.location.reload();
				api.toast({
					msg : "删除成功"
				});
			});
		});
	};
	var buildNothingDiv = function(msg) {
		if (msg) {
			var hint = msg;
		} else {
			var hint = "暂无消息！";
		}
		var tmp = [];
		tmp.push('<div class="none_data">');
		tmp.push('<span>' + hint + '</span>');
		tmp.push('</div>');
		return tmp.join('');
	};
</script>
</html>